# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
import datetime
import logging
from cnblog import settings


class FilePipeline(object):
    def process_item(self, item, spider):

        data = ''

        with open('cnblog.txt', 'a', encoding='utf-8') as f:
            titles = item['title']
            links = item['link']
            for i, j in zip(titles, links):
                data += i+'     '+j+'\n'
            f.write(data)
            f.close()
        return item


class mysqlPipeline(object):
    def __init__(self):
        self.connect = pymysql.connect(
            host = settings.MYSQL_HOST,
            db = settings.MYSQL_DBNAME,
            user = settings.MYSQL_USER,
            passwd = settings.MYSQL_PASSWORD,
            charset = 'utf8',
            use_unicode = True
        )
        self.cursor = self.connect.cursor();
    def process_item(self,item,spider):
        titles = item['title']
        links = item['link']
        for i, j in zip(titles, links):
            try:
                self.cursor.execute(
                    'insert into scrapy_blog (title, url) values (%s, %s) on duplicate key update title=(title)',
                    (i,
                     j)
                )
                self.connect.commit()
            except Exception as error:
                logging.log(error)
                return item

            def close_spider(self,spider):
                self.connect.close();






